package cn.itsource.shop.service.impl;

import cn.itsource.basic.exception.CustomException;
import cn.itsource.basic.exception.ErrorCode;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.BaiduAuditUtils;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.shop.domain.Shop;
import cn.itsource.shop.domain.ShopAuditLog;
import cn.itsource.shop.mapper.ShopAuditLogMapper;
import cn.itsource.shop.mapper.ShopMapper;
import cn.itsource.shop.service.IShopService;
import cn.itsource.basic.base.service.impl.BaseServiceImpl;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.swing.undo.CannotUndoException;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author h
 * @since 2023-07-05
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;


    @Override
    @Transactional
    public AjaxResult settlement(Shop shop) {
        //文本审核
//        Boolean flag = BaiduAuditUtils.TextCensor(shop.getName());
//        if(!flag){
//            throw new CustomException(ErrorCode.CONTENT_ILLEGAL.getCode());
//        }

        //图片审核逻辑
//        String urlPrefix = "https://service-file-primary.java.itsource.cn";
//        String imgUrl = urlPrefix+shop.getLogo();
//        Boolean aBoolean = BaiduAuditUtils.ImgCensor(imgUrl);
//        if(!aBoolean){
//            throw new CustomException(ErrorCode.CONTENT_ILLEGAL.getCode());
//        }
        //保存shop
        shop.setRegisterTime(new Date());
        shop.setState(2);
        insert(shop);
        //保存emp
        Employee admin = shop.getAdmin();
        Employee employee = employeeMapper.loadByUserName(admin.getUsername());
        //判断用户是否已经存在
        if(employee != null){
            throw new CustomException(ErrorCode.USER_ALREADY_EXIST.getCode());
        }
        admin.setState(0);
        admin.setShopId(shop.getId());
        employeeMapper.insert(admin);
        //更新shop
        shop.setAdminId(admin.getId());
        update(shop);
        //发送激活邮件 或者 短信通知

        return AjaxResult.me();
    }

    @Override
    @Transactional
    public AjaxResult audit(Map<String, Object> map) throws MessagingException {
        //取参数
        Long shopId = Long.valueOf(map.get("shop_id").toString());
        Integer state = Integer.valueOf(map.get("state").toString());
        String note = map.get("note").toString();

        //查询店铺并跟新状态
        Shop shop = findOne(shopId);
        shop.setState(state);
        update(shop);
        //创建并保存审核日志对象
        ShopAuditLog log = new ShopAuditLog();
        log.setNote(note);
        log.setAuditId(334L);
        log.setAuditTime(new Date());
        log.setShopId(shopId);
        log.setState(state);
        shopAuditLogMapper.insert(log);
        if(state == 1){
            //通过
            //发邮件 激活账号
            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("hm3761176@vip.qq.com");
            helper.setSubject("店铺入驻成功");

            String url = "<a href='http://localhost:8080/employee/active/"+shop.getAdminId()+"'>点击激活账号</a>";
            //第二个参数 true 代表是否是一个html内容
            helper.setText("恭喜您，店铺入驻成功，请点击链接激活账号"+url,true);
            //收件人
            helper.setTo("hm3761176@vip.qq.com");
            //发送邮件
            javaMailSender.send(mimeMessage);
        }else{
            //驳回

        }
        return AjaxResult.me();
    }

}
